using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class MusicMgr : Singleton<MusicMgr>
{
    private AudioSource bkMusic = null;
    private float bkVaule = 1f;
    private float soundVaule = 1f;
    private GameObject soundObj = null;
    private List<AudioSource> soundList = new List<AudioSource>();

    //构造函数
    public MusicMgr()
    {
        
        MonoMgr.Instance().AddUpdateListener(update);
    }
    private void update()
    {
        for(int i = soundList.Count - 1; i >= 0; i++)
        {
            if (!soundList[i].isPlaying)
            {
                GameObject.Destroy(soundList[i]);
                soundList.RemoveAt(i);
            }
        }
    }
    /// <summary>
    /// 播放背景音乐
    /// </summary>
    /// <param name="name"></param>
    public void PlayBKMusic(string name)
    {
        if(bkMusic==null)
        {
            GameObject obj = new GameObject("BKMusic");
            bkMusic = obj.AddComponent<AudioSource>();
        }
        //异步加载背景音乐并且加载完成后播放
        ResLoad.Instance().LoadAsync<AudioClip>("Music/BK/" + name,(cilp)=> 
        {
            bkMusic.clip = cilp;
            bkMusic.loop = true;
            //调整大小
            bkMusic.volume = bkVaule;
            bkMusic.Play();
        });
    }
    /// <summary>
    /// 改变音量大小
    /// </summary>
    /// <param name="v"></param>
    public void ChangeBKValue(float v)
    {
        bkVaule = v;
        if (bkMusic == null)
            return;
        bkMusic.volume = bkVaule;
    }
    /// <summary>
    /// 暂停背景音乐
    /// </summary>
    public void PauseBKMusic()
    {
        if (bkMusic == null)
            return;
        bkMusic.Pause();
    }
    /// <summary>
    /// 停止背景音乐
    /// </summary>
    public void StopBKMusic()
    {
        if (bkMusic == null)
            return;
        bkMusic.Stop();
    }
    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="name"></param>
    /// <param name="isLoop"></param>
    /// <param name="callBack"></param>
    public void PlaySound(string name,bool isLoop,UnityAction<AudioSource> callBack = null)
    {
        if (soundObj == null)
        {
            soundObj = new GameObject();
            soundObj.name = "Sounds";
        }
        AudioSource source = soundObj.AddComponent<AudioSource>();
        ResLoad.Instance().LoadAsync<AudioClip>("Music/Sounds/"+name,(cilp)=> 
        {
            source.clip = cilp;
            source.loop = isLoop;
            //调整大小
            source.volume = soundVaule;
            source.Play();

            soundList.Add(source);
            if (callBack != null)
                callBack(source);
        });
    }
    /// <summary>
    /// 改变所有音效大小
    /// </summary>
    /// <param name="value"></param>
    public void ChangeSoundValue(float value)
    {
        soundVaule = value;
        for(int i = 0; i < soundList.Count; ++i)
        {
            soundList[i].volume = value;
        }
    }
    public void ChangeSoundValue(float value,int i)
    {
        soundVaule = value;
        soundList[i].volume = value;
    }
    /// <summary>
    /// 停止音效
    /// </summary>
    /// <param name="audioSource"></param>
    public void StopSound(AudioSource audioSource)
    {
        if (soundList.Contains(audioSource))
        {
            soundList.Remove(audioSource);
            audioSource.Stop();
            GameObject.Destroy(audioSource);
        }
    }
}
